package cn.cqy.poi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.cqy.poi.bean.Course;
import cn.cqy.poi.bean.UserImportExcelVO;
import cn.hutool.json.JSONUtil;
import com.github.jsonzou.jmockdata.JMockData;
import com.github.jsonzou.jmockdata.TypeReference;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author caiqy
 * @date 2020/9/17 16:12
 */
public class MainEntrance {

    // 测试数据导出
    @SneakyThrows
    public static void 测试数据导出() {
        List<Course> courseList = JMockData.mock(new TypeReference<List<Course>>() {
        });
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"),
                Course.class, courseList);
        File file = new File("/Users/caiqy/git/excel-test/src/main/resources/test.xlsx");
        OutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    @SneakyThrows
    public static void 测试数据导入() {
        File file = new File("/Users/caiqy/git/excel-test/src/main/resources/test.xlsx");
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(2);
        importParams.setHeadRows(2);
        List<Course> courses = ExcelImportUtil.importExcel(file, Course.class, importParams);
        System.out.println(JSONUtil.toJsonPrettyStr(courses));
    }

    @SneakyThrows
    public static void 测试数据导出2() {
        List<UserImportExcelVO> courseList = JMockData.mock(new TypeReference<List<UserImportExcelVO>>() {
        });
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"),
                UserImportExcelVO.class, courseList);
        File file = new File("/Users/caiqy/git/excel-test/src/main/resources/salus-test.xlsx");
        OutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    @SneakyThrows
    public static void 测试数据导入2() {
        File file = new File("/Users/caiqy/git/excel-test/src/main/resources/salus-test.xlsx");
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(2);
        importParams.setHeadRows(2);
        List<UserImportExcelVO> courses = ExcelImportUtil.importExcel(file, UserImportExcelVO.class, importParams);
        System.out.println(JSONUtil.toJsonPrettyStr(courses));
    }


    @SneakyThrows
    public static void main(String[] args) {
        测试数据导出2();
    }
}
